package com.xk.meeting.controller;

import com.xk.meeting.entity.Regform;
import com.xk.meeting.entity.User;
import com.xk.meeting.service.RegformService;
import com.xk.meeting.service.RegformServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/regForm")
public class RegFormServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> regformMap = request.getParameterMap();
        Regform regform = new Regform();
        try {
            BeanUtils.populate(regform,regformMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        User user = (User)request.getSession().getAttribute("user");
        if (user != null){
            regform.setUid(user.getId());
            RegformService regformService = new RegformServiceImpl();
            int count = regformService.addRegform(regform);
            if (count > 0){
                request.setAttribute("msg","已发送登记信息,请等待审核通过");
                request.getRequestDispatcher("regform.jsp").forward(request,response);
            }else {
                response.sendRedirect("errorPage.jsp");
            }
        }else {
            response.sendRedirect("login.jsp");
        }
    }
}
